package com.fadadianqi.mall.dao.ibatis;

import com.fadadianqi.commons.GenericDAOSuport;
import com.fadadianqi.mall.dao.NavDAO;
import com.fadadianqi.mall.model.Nav;
import com.yeqiangwei.commons.util.DateTimeUtils;
import com.yeqiangwei.commons.util.Pagination;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class NavDAOImpl extends GenericDAOSuport<String, Nav> implements NavDAO {

    @Override
    protected Class<?> getObjectClass() {
        return Nav.class;
    }

    @Override
    protected String getTableName() {
        return "mall_nav";
    }

    @Override
    public void updateOrderForLayer(Nav nav) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orderby", nav.getOrderby());
        map.put("layer", nav.getLayer());
        getSqlMapClientTemplate().update(getNamespace() + ".updateOrderForLayer", nav);
    }

    @Override
    public List<Nav> findRootItems() {
        Map<String, Object> map = new HashMap<String, Object>();
        Pagination pagination = new Pagination(1, Integer.MAX_VALUE);
        pagination.setOrderList(new Pagination.Order[]{
                Pagination.getNewOrder("layer", Boolean.TRUE)
                , Pagination.getNewOrder("orderby", Boolean.TRUE)
        });
        map.put("pagination", pagination);
        long stime = DateTimeUtils.now();
        map.put(START_ROW_NUM, pagination.getStartRows());
        map.put(MAX_ROW_NUM, pagination.getMaxResults());
        map.put(END_ROW_NUM, pagination.getStartRows() + pagination.getMaxResults());
        List<Pagination.Order> orderList = pagination.getOrderList();
        if (ValidatorHelper.isNotEmpty(orderList)) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < orderList.size(); i++) {
                Pagination.Order order = orderList.get(i);
                sb.append(order.getName());
                if (order.getAscending()) {
                    sb.append(" ASC");
                } else {
                    sb.append(" DESC");
                }
                if (i != (orderList.size() - 1)) {
                    sb.append(",");
                }
            }
            map.put(ORDER_STR, sb.toString());
        }
        List<Nav> list = getSqlMapClientTemplate().queryForList(getNamespace() + ".findRootItems", map);
        getLog().info("findRootItems(Map<String, Object> map): proceed in " + (DateTimeUtils.now() - stime));
        return list;
    }

    @Override
    public List<Nav> findChildrenByParent(Nav parent) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rootId", parent.getRootId());
        map.put("parentId", parent.getId());
        Pagination pagination = new Pagination(1, Integer.MAX_VALUE);
        pagination.setOrderList(new Pagination.Order[]{
                Pagination.getNewOrder("layer", Boolean.TRUE)
                , Pagination.getNewOrder("orderby", Boolean.TRUE)
        });
        map.put("pagination", pagination);
        return super.findByPropertys(map);
    }

    @Override
    public List<Nav> findAll() {
        return super.findAll(null);
    }

    @Override
    public Nav findUpperNav(String parentId, int orderby) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orderby", orderby);
        map.put("parentId", parentId);
        return super.getByPropertys(map);
    }

    @Override
    public Nav findLowerNav(String parentId, int orderby) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orderby", orderby);
        map.put("parentId", parentId);
        return super.getByPropertys(map);
    }

    @Override
    public List<Nav> findNavByLayer(int layer) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("layer", layer);
        return super.findByPropertys(map);
    }


}
